package com.oneraku.service.listener;

import com.alibaba.fastjson.JSONObject;
import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import com.oneraku.service.domain.VerifyCodeRecord;
import com.oneraku.service.service.IVerifyCodeRecordService;
import com.oneraku.service.utils.Const;
import com.oneraku.service.utils.RequestUserUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.event.EventListener;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

/**
 * @Author: hui.ren@huberbuy.com
 * @Date: 2019-09-05 21:15
 */
@Slf4j
@Component
public class SmsListener {
    private IVerifyCodeRecordService verifyCodeRecordService;
    @Autowired
    public SmsListener(IVerifyCodeRecordService verifyCodeRecordService) {
        this.verifyCodeRecordService = verifyCodeRecordService;
    }
    @Async
    @EventListener
    public void processBatchSendCouponEvent(SmsEvent smsEvent) {
        DefaultProfile profile = DefaultProfile.getProfile("default", "LTAI4FrxKQJC6jvLeRfo5FCJ", "JxWp3mUBG1ynqWs9S5nW29XJJs3S95");
        IAcsClient client = new DefaultAcsClient(profile);
        CommonRequest request = new CommonRequest();
        request.setMethod(MethodType.POST);
        request.setDomain("dysmsapi.aliyuncs.com");
        request.setVersion("2017-05-25");
        request.setAction("SendSms");
        request.putQueryParameter("RegionId", "default");
        request.putQueryParameter("PhoneNumbers", smsEvent.getPhoneNumber());
        request.putQueryParameter("SignName", "ninjacask");
        request.putQueryParameter("TemplateCode", "SMS_173477594");
        JSONObject templateParam = new JSONObject();
        templateParam.put("code",smsEvent.getVerifyCode());
        request.putQueryParameter("TemplateParam", templateParam.toJSONString());
        try {
            CommonResponse response = client.getCommonResponse(request);
//            {"Message":"OK","RequestId":"5C10EB1E-1672-4D4D-8A44-866E2EE63691","BizId":"376721083904980364^0","Code":"OK"}
            JSONObject object = JSONObject.parseObject(response.getData());
            if(object.getString("Code").equals("OK")){
                VerifyCodeRecord record =  new VerifyCodeRecord();
                record.setCode(smsEvent.getVerifyCode());
                record.setExpire(System.currentTimeMillis()+ Const.EXPIRE_VERIFY);
                record.setUid(RequestUserUtils.getUser().getId());
                record.setType(smsEvent.getType());
                record.setNumber(smsEvent.getPhoneNumber());
                verifyCodeRecordService.save(record);
            }
            log.info(response.getData());
        } catch (ServerException e) {
            e.printStackTrace();
        } catch (ClientException e) {
            e.printStackTrace();
        }
    }
}
